/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* SdProjectResourcePoolService
* SD部门审批人资源池
*
* 修改纪录
* 2021-12-07 版本：1.0 jiangtao 创建。
* @version 版本：1.0
* @author 作者：jiangtao
* 创建日期：2021-12-07
*/
package com.topscomm.gpm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.gpm.pojo.GpmProjectResourcePoolEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.gpm.service.IGpmProjectResourcePoolService;
import com.topscomm.gpm.service.impl.auto.GpmProjectResourcePoolServiceAuto;

import java.util.List;
import java.util.Map;

@Service(value = "gpmProjectResourcePoolService")
@Scope(value = "singleton")
public class GpmProjectResourcePoolService extends GpmProjectResourcePoolServiceAuto implements IGpmProjectResourcePoolService {
	@Autowired
	private CboUserService cboUserService;
	@Autowired
	private CboDeptService cboDeptService;
	@Override protected void updateBefore(Map<String, Object> map) throws ServiceException {
		super.updateBefore(map);
		String usercode = ConvertUtil.convertToString(map.get("usercode"));
		map.put("deptcode",this.queryDeptCode(usercode));
		String deptname = ConvertUtil.convertToString(map.get("deptname"));
		String projectlevel = ConvertUtil.convertToString(map.get("projectlevel"));
		String projecttype = ConvertUtil.convertToString(map.get("projecttype"));
		this.checkIsRight(projectlevel,projecttype,deptname,usercode);

	}

	@Override protected void beforeInsert(BasicEntity entity) throws ServiceException {
		super.beforeInsert(entity);
		String usercode = ConvertUtil.convertToString(entity.getValue("usercode"));
		String deptname = ConvertUtil.convertToString(entity.getValue("deptname"));
		String projectlevel = ConvertUtil.convertToString(entity.getValue("projectlevel"));
		String projecttype = ConvertUtil.convertToString(entity.getValue("projecttype"));
		this.checkIsRight(projectlevel,projecttype,deptname,usercode);
		entity.setValue(GpmProjectResourcePoolEntity.FieldDeptcode,this.queryDeptCode(usercode));
	}

	/***
	 * @description: 根据工号查询部门的deptcode
	 * @param usercode
	 * @return
	 * @author: jiangtao1
	 * @date: 2021/12/7 15:49
	 * @modify:
	 */
	private String queryDeptCode(String usercode) {
		CboUserEntity cboUserEntity = cboUserService.queryObjectByWhere(" code = '"+usercode+"'");
		if(cboUserEntity == null) {
			return "";
		}
		String deptid = ConvertUtil.convertToString(cboUserEntity.getDeptid());
		CboDeptEntity cboDeptEntity = cboDeptService.queryById(deptid);
		if(cboDeptEntity == null) {
			return "";
		}
		return cboDeptEntity.getCode();
	}

	private void checkIsRight(String projectlevel,String projecttype,String deptname,String usercode){
		String[] split = projectlevel.split(",");
		StringBufferProxy whereSql = new StringBufferProxy();
		whereSql.appendSingle("1=1");
		whereSql.appendSingle("and projecttype = '{0}'",projecttype);
		whereSql.appendSingle("and deptname like '%{0}%'",deptname);
		whereSql.appendSingle("and usercode  = '{0}'",usercode);
		if(split.length==1){
			whereSql.appendSingle("and (projectlevel like '%{0}%')",split[0]);
		}else{
			for (int i = 0; i < split.length; i++) {
				if (i == 0) {
					whereSql.appendSingle("and ( projectlevel like '%{0}%' or ", split[i]);
				} else if (i == split.length - 1) {
					whereSql.appendSingle(" projectlevel like '%{0}%' )", split[i]);
				} else {
					whereSql.appendSingle(" projectlevel like '%{0}%' or ", split[i]);
				}
			}
		}
		//查询数量
		List<Map<String, Object>> list = this.queryMapByWhere(whereSql.toString());
		if(list.size()>0){
			Map<String, Object> objectMap = list.get(0);
			StringBufferProxy message = new StringBufferProxy();
			message.appendSingle("本条数据和【项目等级：{1}；决策部门：{2}；审批人：{3}】有重复，请检查。",ConvertUtil.convertToString(objectMap.get("projecttype")),
					ConvertUtil.convertToString(objectMap.get("dispprojectlevel")),
					ConvertUtil.convertToString(objectMap.get("deptname")),
					ConvertUtil.convertToString(objectMap.get("username")));
			throw new ServiceException(message.toString());
		}

	}
}
